package com.seguriapp.common.gwt.client;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.regexp.shared.RegExp;

public class UtilClientValidations {

  public static final RegExp DATE_REGEX = RegExp.compile("^[0-3]\\d/[01]\\d/\\d{4}$");
  public static final RegExp EMAIL_REGEX = RegExp.compile("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,4})$");
  public static final RegExp NUMBER_REGEX = RegExp.compile("^[0-9]+$");
  public static final DateTimeFormat DATE_FORMAT = DateTimeFormat.getFormat("dd/MM/yyyy");
  public static final DateTimeFormat DATE_SERVICE_FORMAT = DateTimeFormat.getFormat("ddMMyyyy HH:mm:ss");
  public static final DateTimeFormat DATE_VIEW_FORMAT = DateTimeFormat.getFormat("dd/MM/yyyy HH:mm:ss");
  public static final List<Integer> HOURS_OF_DAY = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
  public static final List<Integer> MINUTES_OF_HOUR = Arrays.asList(0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55);
  public static final String AM_TIME_CONSTANT = "AM";
  public static final String PM_TIME_CONSTANT = "PM";

  public static boolean validateDate(final String text) {
    return DATE_REGEX.test(text);
  }

  public static boolean isEmptyString(final String text) {
    return text == null || text.isEmpty();
  }

  public static boolean validateEmail(final String email) {
    return EMAIL_REGEX.test(email);
  }

  public static boolean validateInteger(final String text) {
    return NUMBER_REGEX.test(text);
  }

  public static boolean validateDNI(final String text) {
    return NUMBER_REGEX.test(text) && text.length() == 8;
  }

  public static boolean validateDateValue(final String dateStr) {
    final Date date = DATE_FORMAT.parse(dateStr);
    return date.before(new Date());
  }

}
